<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>学生主页</title>
    <script src="js/base.js"></script>
    <style>
        /* 全局样式 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            background-color: #f0f2f5;
            height: 100vh;
            display: flex;
        }

        /* 侧边栏样式 */
        .sidebar {
            width: 220px;
            background-color: #2c3e50;
            color: #ecf0f1;
            height: 100%;
            box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
        }

        .sidebar h2 {
            text-align: center;
            padding: 25px 0;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            margin-bottom: 20px;
            font-size: 1.5rem;
        }

        .sidebar a {
            display: block;
            color: #ecf0f1;
            text-decoration: none;
            padding: 15px 25px;
            margin: 5px 0;
            transition: all 0.3s ease;
            font-size: 1rem;
            border-left: 3px solid transparent;
        }

        .sidebar a:hover {
            background-color: #34495e;
            padding-left: 30px;
        }

        .sidebar a.active {
            background-color: #3498db;
            border-left: 3px solid #2980b9;
        }

        /* 主内容区样式 */
        .main-content {
            flex: 1;
            padding: 30px;
            overflow-y: auto;
        }

        h1 {
            font-size: 2rem;
            color: #2c3e50;
            margin: 0 0 30px 0;
            padding-bottom: 15px;
            border-bottom: 1px solid #ddd;
        }

        h4 {
            font-size: 1.3rem;
            color: #34495e;
            margin: 25px 0;
            font-weight: 600;
        }

        #sname {
            color: #e74c3c;
            font-weight: bold;
        }

        /* 表格样式 */
        .table-container {
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            overflow: hidden;
            margin-bottom: 30px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            text-align: center;
        }

        th, td {
            padding: 14px 12px;
            border-bottom: 1px solid #eee;
        }

        th {
            background-color: #2980b9;
            color: white;
            font-weight: 600;
        }

        tr:hover {
            background-color: #f8f9fa;
        }

        td a {
            color: #3498db;
            text-decoration: none;
            font-weight: 600;
            padding: 5px 10px;
            border-radius: 4px;
            transition: all 0.2s ease;
        }

        td a:hover {
            color: #2980b9;
            background-color: #e8f4fd;
            text-decoration: none;
        }

        /* 按钮样式 */
        .button-container {
            display: flex;
            justify-content: center;
            gap: 12px;
            margin: 25px 0;
        }

        button {
            padding: 10px 20px;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 0.95rem;
            transition: all 0.2s ease;
        }

        button:hover {
            background-color: #2980b9;
            transform: translateY(-2px);
        }

        button:disabled {
            background-color: #bdc3c7;
            cursor: not-allowed;
            transform: none;
        }

        #buttons {
            display: flex;
            justify-content: center;
            gap: 10px;
            margin-top: 20px;
        }

        #buttons button {
            padding: 8px 15px;
        }
    </style>
</head>
<body>
<!-- 左侧导航栏 -->
<div class="sidebar">
    <h2>学生选课系统</h2>
    <a href="studentIndex.html" class="active">课程列表</a>
    <a href="studentManagerCourse.html">已选课程管理</a>
    <a href="signIn.html">退出登录</a>
</div>

<!-- 主内容区 -->
<div class="main-content">
    <h1>欢迎<span id="sname"></span>登录</h1>
    <script>
        document.getElementById("sname").innerHTML = localStorage.getItem("sname");
    </script>

    <h4>学生选课</h4>

    <div class="table-container">
        <table id="courseData" border="0">
            <tr>
                <td>课程号</td>
                <td>课程名称</td>
                <td>授课教师</td>
                <td>教室容量</td>
                <td>当前选课人数</td>
                <td colspan="3" align="center">操作</td>
            </tr>
        </table>
    </div>

    <div id="buttons"></div>

    <script>
        let studentId = localStorage.getItem("sid");
        let pageNo = 1;
        let pageSize = 5;
        let pageCount = 0;
        let arr = null;

        function loadData() {
            let url = "http://localhost:8080/student/page?pageNo=" + pageNo + "&pageSize=" + pageSize + "&studentId=" + studentId + "&state=0";
            $.rest(url, "get", null, function (r) {
                if (r.code == 200) {
                    arr = r.data.data;
                    pageCount = r.data.pageCount;
                    let courseDataInnerHTML = "<tr>\n" +
                        "            <td>课程号</td>\n" +
                        "            <td>课程名称</td>\n" +
                        "            <td>授课教师</td>\n" +
                        "            <td>教室容量</td>\n" +
                        "            <td>当前选课人数</td>\n" +
                        "            <td colspan=\"3\">操作</td>\n" +
                        "        </tr>";
                    for (let i = 0; i < arr.length; i++) {
                        let item = arr[i];
                        courseDataInnerHTML += "<tr>" +
                            "<td>" + item.id + "</td>" +
                            "<td>" + item.name + "</td>" +
                            "<td>" + item.tname + "</td>" +
                            "<td>" + item.maxLimit + "</td>" +
                            "<td>" + item.count + "</td>" +
                            "<td colspan='3'><a href='#' onclick='return onSelectedClick(" + (i) + ")'>选择这门课程</a></td>" +
                            "</tr>";
                    }
                    document.getElementById("courseData").innerHTML = courseDataInnerHTML;

                    let buttonsInnerHTML = "";
                    if (pageCount > 0) {
                        buttonsInnerHTML = "<button onclick='onPrevClick();' " + (pageNo == 1 ? "disabled" : "") + ">上一页</button>";
                        for (let i = 0; i < pageCount; i++) {
                            buttonsInnerHTML += "<button onclick='onPageClick(" + (i + 1) + ")' " + (pageNo == i + 1 ? "style='background-color:#2980b9'" : "") + ">" + (i + 1) + "</button>";
                        }
                        buttonsInnerHTML += "<button onclick='onNextClick();' " + (pageNo == pageCount ? "disabled" : "") + ">下一页</button>";
                    }
                    document.getElementById("buttons").innerHTML = buttonsInnerHTML;
                }
            });
        }
        loadData();

        function onPageClick(pNo) {
            pageNo = pNo;
            loadData();
        }

        function onPrevClick() {
            if (pageNo == 1) {
                return;
            }
            pageNo--;
            loadData();
        }

        function onNextClick() {
            if (pageNo == pageCount) {
                return;
            }
            pageNo++;
            loadData();
        }

        function onSelectedClick(index) {
            let item = arr[index];
            let courseId = item.id;
            let url = "http://localhost:8080/student/editCourseState?studentId=" + studentId + "&courseId=" + courseId + "&state=1";
            $.rest(url, "get", null, function (r) {
                if (r.code == 200) {
                    alert("选课成功");
                } else {
                    alert("选课失败,每人至多选择五门课程");
                }
                loadData();
            });
        }
    </script>
</div>
</body>
</html>